import asyncio
import time
from concurrent.futures import ProcessPoolExecutor
from asyncio.events import AbstractEventLoop
from functools import partial
from typing import List


def count(count_to):
    start = time.time()
    counter = 0
    while counter < count_to:
        counter += 1
    end = time.time()
    print(f"计算{count_to}一共花费时间为：{end - start}s")
    return counter


async def main():
    with ProcessPoolExecutor() as process_pool:
        # 使用其参数为倒计时创建一个偏应用的函数
        loop: AbstractEventLoop = asyncio.get_running_loop()
        numbers = [1, 3, 5, 22, 100000000]
        # 将每个调用提交到进程池，并将其附加到列表中
        calls = [partial(count, num) for num in numbers]
        call_coros = []

        for call in calls:
            call_coros.append(loop.run_in_executor(process_pool, call))
        # 等待所有结果完成
        result = await asyncio.gather(*call_coros)
        for res in result:
            print(res)


if __name__ == '__main__':
    asyncio.run(main())
